#ifndef UARTCOMM_HPP
#define UARTCOMM_HPP

#include "comm_interface.hpp"
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>

class UartComm : public CommInterface {
public:
    explicit UartComm(const std::string& port, unsigned int baudrate);
    ~UartComm();
    
    bool initialize() override;
    bool send(const std::vector<uint8_t>& data) override;
    std::vector<uint8_t> receive() override;
    void registerCallback(DataReceivedCallback callback) override;
    std::string getName() const override;
    bool isConnected() const override;
    void shutdown() override;
    
private:
    void readThreadFunc();
    void processReceivedData();
    
    std::string port_;
    unsigned int baudrate_;
    int fd_ = -1;
    std::atomic<bool> running_{false};
    
    std::thread readThread_;
    std::thread processThread_;
    std::mutex dataMutex_;
    std::condition_variable dataCond_;
    
    std::queue<std::vector<uint8_t>> receiveQueue_;
    
    DataReceivedCallback callback_;
    
    static constexpr size_t BUFFER_SIZE = 4096;
    uint8_t ringBuffer_[BUFFER_SIZE];
    size_t head_ = 0;
    size_t tail_ = 0;
};

#endif // UARTCOMM_HPP